//
//  UIImage+JwSkiner.m
//  JwPack
//
//  Created by 陈警卫 on 2024-08-07.
//

#import "UIImage+JwSkiner.h"
#import "JwSkiner.h"

@implementation UIImage (JwSkiner)

/// 根据kJwSkiner.skinImageEx获取图片
+ (UIImage *)jw_imageSkinWithName:(NSString *)name {
    if (!name) {
        return nil;
    }
    UIImage *image = [UIImage imageNamed:name];
    if (kJwSkiner.skinImageEx == JwSkinerImageExXcassets) {
        image = [self jw_imageXcassetsSkinWithName:name];
    } else if (kJwSkiner.skinImageEx == JwSkinerImageExBundle) {
        image = [self jw_imageBundleSkinWithName:name];
    }
    image = [image imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    return image;
}

/// 根据JwSkinerImageExXcassets获取图片
+ (UIImage *)jw_imageXcassetsSkinWithName:(NSString *)name {
    UIImage *image = [UIImage imageNamed:name];
    NSString *suff = kJwSkiner.currentSkinImageSuffixKey;
    NSString *imagename = nil;
    if (suff.length > 0) {
        imagename = [NSString stringWithFormat:@"%@_%@", name, suff];
    } else {
        imagename = [NSString stringWithFormat:@"%@_%@", name, kJwSkiner.currentSkinKey];
    }
    image = [UIImage imageNamed:imagename] ?: image;
    image = [image imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    return image;
}

/// 根据JwSkinerImageExBundle获取图片
+ (UIImage *)jw_imageBundleSkinWithName:(NSString *)name {
    NSString *bundlename = [NSString stringWithFormat:@"%@.bundle", kJwSkiner.currentSkinKey];
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:bundlename];
    NSBundle *bundle = [NSBundle bundleWithPath:path];
    UIImage *image = [UIImage imageNamed:name inBundle:bundle compatibleWithTraitCollection:nil];
    image = [image imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    return image;
}

@end
